package com.cloud.cloudcommons.controller;

import org.springframework.http.HttpRequest;
import org.springframework.session.config.annotation.web.server.EnableSpringWebSession;
import org.springframework.session.data.redis.RedisFlushMode;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @Aurher: QiaoHang
 * @Description:    配置spring data redis session，最大的session保存时间60*10秒，刷新session的策略：IMMEDIATE(立即)
 * @Data: 2019/9/16 13:52
 * @Modified By:
 */
@RestController
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=60*10,redisFlushMode= RedisFlushMode.IMMEDIATE)
public class SpringSessionController{

    @RequestMapping("/sessionSet.do")
    public String setToSession(String str, HttpServletRequest request){
        HashMap<String, Object> map = new HashMap<>();
        map.put("str",str);
        map.put("sessionId",request.getSession().getId());
        map.put("requestURL",request.getRequestURI());
        request.getSession().setAttribute("commonSession",map);
        return  "SUCCESS!";
    }


    @RequestMapping("/sessionGet.do")
    public Object sessionGet(HttpServletRequest request){
        String zuulHeader = request.getHeader("zuul-header-name");
        System.out.println("zuulHeader : "+zuulHeader);
        System.out.println("sessionid="+ request.getSession().getId());
        Object commonSession = request.getSession().getAttribute("commonSession");
        return  commonSession;
    }
}
